package com.yykj.hospital.server.controller;

import com.yykj.common.util.FileWithExcelUtil;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.server.dto.train.FormTrainLogListDTO;
import com.yykj.hospital.server.form.train.FormTrainLogSearchForm;
import com.yykj.hospital.server.form.train.FormTrainSearchForm;
import com.yykj.hospital.server.form.train.TrainFeedbackStatusForm;
import com.yykj.hospital.server.model.entity.form.template.FormTrain;
import com.yykj.hospital.server.model.entity.form.template.FormTrainDetail;
import com.yykj.hospital.server.model.entity.form.template.FormTrainType;
import com.yykj.hospital.server.service.from.template.FormTrainService;
import com.yykj.hospital.server.service.from.template.FormTrainTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/backstage/hospital/from/train")
@SuppressWarnings("unchecked")
@Api(tags = "医院后台 -- 直通车相关接口")
public class FormTrainBackStageController {

    //========================= 直通车类型相关start ======================================================================
    @ApiOperation("直通车 -- 类型 -- 创建和修改")
    @PostMapping("/type/edit")
    public ResultVO<FormTrainType> editFormTrainType(@RequestBody FormTrainType formTrainType) {
        return ResultVO.success(formTrainTypeService.editFormTrainType(formTrainType));
    }

    @ApiOperation("直通车 -- 类型 -- 删除")
    @DeleteMapping("/type/delete/{id}")
    public ResultVO deleteFormTrainType(@PathVariable Integer id) {
        formTrainTypeService.deleteFormTrainType(id);
        return ResultVO.success();
    }

    @ApiOperation("直通车 -- 类型 -- 列表")
    @GetMapping("/type/list/{hospitalId}")
    public ResultVO<List<FormTrainType>> listFormTrainType(@PathVariable Integer hospitalId) {
        return ResultVO.success(formTrainTypeService.listFormTrainType(hospitalId));
    }
    //===================== 直通车类型相关 end ==========================================================================

    //===================== 直通车 相关 start ==========================================================================
    @ApiOperation("直通车 -- 创建和修改")
    @PostMapping("/edit")
    public ResultVO<FormTrain> editFormTrain(@Valid @RequestBody FormTrain formTrain) {
        return ResultVO.success(formTrainService.editFormTrain(formTrain));
    }

    @ApiOperation("直通车 -- 删除")
    @DeleteMapping("/delete/{id}")
    public ResultVO<FormTrain> deleteFormTrain(@PathVariable Integer id) {
        formTrainService.deleteFormTrain(id);
        return ResultVO.success();
    }

    @ApiOperation("直通车 -- 列表")
    @GetMapping("/list")
    public ResultVO<PageData<FormTrain>> listFormTrain(FormTrainSearchForm searchForm) {
        return ResultVO.success(formTrainService.listFormTrain(searchForm));
    }

    //===================== 直通车 相关 end ==========================================================================

    //===================== 直通车 明细字段 start ====================================================================

    @ApiOperation("直通车 -- 明细 -- 创建和修改")
    @PostMapping("/detail/edit")
    public ResultVO<FormTrain> editFormTrainDetail(@Valid @RequestBody FormTrainDetail detail) {
        return ResultVO.success(formTrainService.editFormTrainDetail(detail));
    }

    @ApiOperation("直通车 -- 明细 -- 删除")
    @DeleteMapping("/detail/delete/{id}")
    public ResultVO<FormTrain> deleteFormTrainDetail(@PathVariable Integer id) {
        formTrainService.deleteFormTrainDetail(id);
        return ResultVO.success();
    }

    @ApiOperation("直通车 -- 明细 -- 列表")
    @GetMapping("/detail/list/{id}")
    public ResultVO<List<FormTrainDetail>> listFormTrainDetail(@PathVariable Integer id) {
        return ResultVO.success(formTrainService.listFormTrainDetail(id));
    }


    //===================== 直通车 明细字段 end ====================================================================

    //===================== 直通车 用户反馈 ====================================================================
    @ApiOperation("直通车 -- 用户反馈 -- 列表")
    @GetMapping("/user/feedback/list")
    public ResultVO<PageData<FormTrainLogListDTO>> listFormTrainFeedbackLog(FormTrainLogSearchForm form) {
        return ResultVO.success(formTrainService.listFormTrainFeedbackLog(form));
    }

    @ApiOperation("直通车 -- 用户反馈 -- 列表 -- 导出")
    @GetMapping("/user/feedback/export")
    public void exportTrainFeedbackLog(HttpServletResponse response,
                                           @RequestParam(name = "hospitalId", required = true) Integer hospitalId,
                                           @RequestParam(name = "trainId", required = false) Integer trainId,
                                           @RequestParam(name = "patientName", required = false) String patientName,
                                           @RequestParam(name = "startDate", required = false) String startDate,
                                           @RequestParam(name = "endDate", required = false) String endDate
                                           ) {

        FormTrainLogSearchForm form = FormTrainLogSearchForm.builder()
                .hospitalId(hospitalId)
                .trainId(trainId)
                .patientName(patientName)
                .startTime(startDate)
                .endTime(endDate)
                .build();

        Workbook workbook = formTrainService.exportTrainFeedbackLog(form);
        FileWithExcelUtil.downLoadExcel("用户反馈", response, workbook);
    }


//    @ApiOperation("直通车 -- 用户反馈 -- 字段详情")
//    @GetMapping("/user/feedback/detail/{logId}")
//    public ResultVO<List<FormTrainFeedbackDetailDTO>> listFormTrainFeedbackDetail(@PathVariable Integer logId) {
//        return ResultVO.success(formTrainService.listFormTrainFeedbackDetail(logId));
//    }

    @ApiOperation("直通车 -- 用户反馈 -- 修改处理状态")
    @PostMapping("/user/feedback/finish")
    public ResultVO changeStatusTrainFeedback(@RequestBody TrainFeedbackStatusForm form) {
        formTrainService.changeStatusTrainFeedback(form);
        return ResultVO.success();
    }




    private FormTrainTypeService formTrainTypeService;

    private FormTrainService formTrainService;

    @Autowired
    public void setFormTrainTypeService(FormTrainTypeService formTrainTypeService) {
        this.formTrainTypeService = formTrainTypeService;
    }
    @Autowired
    public void setFormTrainService(FormTrainService formTrainService) {
        this.formTrainService = formTrainService;
    }
}
